package com.veryGood.service.Impl;

import com.veryGood.common.constant.Constants;
import com.veryGood.common.result.LayuiResultJson;
import com.veryGood.common.result.R;
import com.veryGood.domain.pojo.PurchaseInfo;
import com.veryGood.mapper.PurMngMapper;
import com.veryGood.service.PurMngService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PurMngServiceImpl implements PurMngService {
    @Autowired
    private PurMngMapper purMngMapper;
    /**
     * 添加采购订单
     * @param purchaseInfo
     * @return
     */
    @Override
    public R addPur(PurchaseInfo purchaseInfo) {
        System.out.println(purchaseInfo);
        return R.success(purMngMapper.addPurchase(purchaseInfo));
    }

    @Override
    public LayuiResultJson findPurAll(Integer page, Integer limit) {
        LayuiResultJson layuiResultJson = new LayuiResultJson();
        //设置分页
        Integer offset = (page - 1)*limit;
        Integer rows = limit;
        //调用mapper
        List<PurchaseInfo> purchaseInfos = purMngMapper.findPurchase(offset,rows);
        for (PurchaseInfo purchaseInfo : purchaseInfos) {
            System.out.println(purchaseInfo.getOrderNumber());
            System.out.println(purchaseInfo.getOrderTime());
        }
        layuiResultJson.setCode(Constants.OK);
        layuiResultJson.setMsg(Constants.MESSAGE_OK);
        Integer totalSize = purMngMapper.countPurchase();
        layuiResultJson.setCount(totalSize);
        layuiResultJson.setData(purchaseInfos);
        return layuiResultJson;
    }

    @Override
    public R delPurById(Integer id) {
        int result = purMngMapper.delPurchaseById(id);
        if(result > 0 ){
            return R.success("删除成功！");
        }
        return R.error();
    }
}
